//
//  SplashChild.m
//
//  Created by lizhenqiang  on 16/4/14
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "SplashChild.h"


NSString *const kSplashChildId = @"_id";
NSString *const kSplashChildLevel = @"level";
NSString *const kSplashChildName = @"name";
NSString *const kSplashChildPid = @"pid";
NSString *const kSplashChildIcon = @"icon";


@interface SplashChild ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SplashChild

@synthesize childIdentifier = _childIdentifier;
@synthesize level = _level;
@synthesize name = _name;
@synthesize pid = _pid;
@synthesize icon = _icon;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.childIdentifier = [self objectOrNilForKey:kSplashChildId fromDictionary:dict];
            self.level = [[self objectOrNilForKey:kSplashChildLevel fromDictionary:dict] doubleValue];
            self.name = [self objectOrNilForKey:kSplashChildName fromDictionary:dict];
            self.pid = [self objectOrNilForKey:kSplashChildPid fromDictionary:dict];
            self.icon = [self objectOrNilForKey:kSplashChildIcon fromDictionary:dict];
        CGFloat width = [NSString calculateTextWidth:30 Content:self.name font:themeFont13];//15
        self.nameWidth = width+20;
    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.childIdentifier forKey:kSplashChildId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kSplashChildLevel];
    [mutableDict setValue:self.name forKey:kSplashChildName];
    [mutableDict setValue:self.pid forKey:kSplashChildPid];
    [mutableDict setValue:self.icon forKey:kSplashChildIcon];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.childIdentifier = [aDecoder decodeObjectForKey:kSplashChildId];
    self.level = [aDecoder decodeDoubleForKey:kSplashChildLevel];
    self.name = [aDecoder decodeObjectForKey:kSplashChildName];
    self.pid = [aDecoder decodeObjectForKey:kSplashChildPid];
    self.icon = [aDecoder decodeObjectForKey:kSplashChildIcon];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_childIdentifier forKey:kSplashChildId];
    [aCoder encodeDouble:_level forKey:kSplashChildLevel];
    [aCoder encodeObject:_name forKey:kSplashChildName];
    [aCoder encodeObject:_pid forKey:kSplashChildPid];
    [aCoder encodeObject:_icon forKey:kSplashChildIcon];
}

- (id)copyWithZone:(NSZone *)zone
{
    SplashChild *copy = [[SplashChild alloc] init];
    
    if (copy) {

        copy.childIdentifier = [self.childIdentifier copyWithZone:zone];
        copy.level = self.level;
        copy.name = [self.name copyWithZone:zone];
        copy.pid = [self.pid copyWithZone:zone];
        copy.icon = [self.icon copyWithZone:zone];
    }
    
    return copy;
}


@end
